/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoexam.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExamType;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExamName;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamNameService;

import java.util.List;

/**
 * 模拟考试名称Controller
 * @author zms
 * @version 2018-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoexam/campusinfoExamName")
public class CampusinfoExamNameController extends BaseController {
	@Autowired
	private CampusinfoExamTypeService campusinfoExamTypeService;
	@Autowired
	private CampusinfoExamNameService campusinfoExamNameService;

	@ModelAttribute
	public CampusinfoExamName get(@RequestParam(required=false) String id) {
		CampusinfoExamName entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoExamNameService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoExamName();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoExamName:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoExamName campusinfoExamName, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoExamName> page = campusinfoExamNameService.findPage(new Page<CampusinfoExamName>(request, response), campusinfoExamName);
		List<CampusinfoExamName> campusinfoExamNames = campusinfoExamNameService.findList(campusinfoExamName);
		model.addAttribute("campusinfoExamNames", campusinfoExamNames);
		model.addAttribute("page", page);
		return "modules/campusinfoexam/campusinfoExamNameList";
	}

	@RequiresPermissions("campusinfoexam:campusinfoExamName:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoExamName campusinfoExamName, Model model) {
		CampusinfoExamType campusinfoExamType = new CampusinfoExamType();
		List<CampusinfoExamType> campusinfoExamTypes = campusinfoExamTypeService.findList(campusinfoExamType);
		model.addAttribute("campusinfoExamTypes", campusinfoExamTypes);
		model.addAttribute("campusinfoExamName", campusinfoExamName);
		return "modules/campusinfoexam/campusinfoExamNameForm";
	}

	@RequiresPermissions("campusinfoexam:campusinfoExamName:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoExamName campusinfoExamName, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoExamName)){
			return form(campusinfoExamName, model);
		}
		campusinfoExamNameService.save(campusinfoExamName);
		addMessage(redirectAttributes, "保存模拟考试名称成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoExamName/?repage";
	}
	
	@RequiresPermissions("campusinfoexam:campusinfoExamName:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoExamName campusinfoExamName, RedirectAttributes redirectAttributes) {
		campusinfoExamNameService.delete(campusinfoExamName);
		addMessage(redirectAttributes, "删除模拟考试名称成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoexam/campusinfoExamName/?repage";
	}

}